//package com.itzdm.filter;
//
//import com.alibaba.fastjson.JSONObject;
//import com.itzdm.pojo.Result;
//import com.itzdm.utils.JwtUtils;
//import jakarta.servlet.*;
//import jakarta.servlet.annotation.WebFilter;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.util.StringUtils;
//
//import java.io.IOException;
//@Slf4j
//@WebFilter(urlPatterns = "/*")
//public class LoginCheckFilter implements Filter {
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        HttpServletRequest req = (HttpServletRequest) servletRequest;
//        HttpServletResponse rep = (HttpServletResponse) servletResponse;
//        String url = req.getRequestURL().toString();
//        log.info("请求的url：{}",url);
//        if (url.contains("login")){
//            log.info("放行...");
//            filterChain.doFilter(servletRequest,servletResponse);
//            return;
//        }
//
//
//        String jwt = req.getHeader("token");
//        if(!StringUtils.hasLength(jwt)){
//            log.info("请求头为空，返回未登录");
//            Result error = Result.error("NOT_LOGIN");
//            String notLogin = JSONObject.toJSONString(error);
//            servletResponse.getWriter().write(notLogin);
//            return;
//        }
//        try {
//            JwtUtils.parseJWT(jwt);
//        } catch (Exception e) {
//            log.info("解析令牌失败，返回未登录的信息");
//            Result error = Result.error("NOT_LOGIN");
//            String notLogin = JSONObject.toJSONString(error);
//            servletResponse.getWriter().write(notLogin);
//            return;
//        }
//        log.info("令牌：{}",jwt);
//        log.info("令牌合法，放行");
//        filterChain.doFilter(servletRequest,servletResponse);
//    }
//
//}
